/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.util;

import java.security.NoSuchAlgorithmException;

/**
 *
 * @author israel
 */
public class HashUtil {

    public static String calculateSecurityHash(String stringInput, String algorithmName)
            throws java.security.NoSuchAlgorithmException {
        String hexMessageEncode = "";
        byte[] buffer = stringInput.getBytes();
        java.security.MessageDigest messageDigest =
                java.security.MessageDigest.getInstance(algorithmName);
        messageDigest.update(buffer);
        byte[] messageDigestBytes = messageDigest.digest();
        for (int index = 0; index < messageDigestBytes.length; index++) {
            int countEncode = messageDigestBytes[index] & 0xff;
            if (Integer.toHexString(countEncode).length() == 1) {
                hexMessageEncode = hexMessageEncode + "0";
            }
            hexMessageEncode = hexMessageEncode + Integer.toHexString(countEncode);
        }
        return hexMessageEncode;
    }

    public static Long genSerial(String nodename) throws NoSuchAlgorithmException {
        String serialNumberEncoded = calculateSecurityHash(nodename, "MD2") + calculateSecurityHash(nodename, "MD5")
                + calculateSecurityHash(nodename, "SHA1");
        return Long.valueOf(serialNumberEncoded);
    }

    public static Long createHash(String hashString) {
//        Long hash = 0L;
//        Long aux = 1L;
//
//        for (int i = 0; i < hashString.length(); i++) {
//            aux = aux + ((int) hashString.charAt(i));
//            hash = hash + aux;
//        }
//        return (hash * 65535) + aux;
        return Long.valueOf(hashString.hashCode());
    }
}
